package cool.zhoujie.pageselect;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;

import java.util.List;
import cool.zhoujie.pageselect.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    private MainViewModel mViewModel;

    private ItemAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mViewModel = new ViewModelProvider(this).get(MainViewModel.class);
        binding.setViewmodel(mViewModel);

        recyclerView = binding.getRoot().findViewById(R.id.list);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(RecyclerView.HORIZONTAL);
        recyclerView.setLayoutManager(manager);

        mViewModel.mList.observe(this, new Observer<List<Selector>>() {
            @Override
            public void onChanged(List<Selector> selectors) {
                mAdapter= new ItemAdapter(selectors, mViewModel);
                mViewModel.setAdapetr(mAdapter);
                recyclerView.setAdapter(mAdapter);
            }
        });

        binding.setLifecycleOwner(this);
    }
}
